﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.SortingCenter;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class SortingCenterController : BaseController
    {
        #region 分拣中心页面 + ActionResult SortingCenterIndex()
        public ActionResult SortingCenterIndex()
        {
            return View();
        }
        #endregion

        #region 分拣中心管理页面获取列表数据 + ActionResult SortingCenterList(SortingCenterModel model)
        /// <summary>
        /// 分拣中心管理页面获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult SortingCenterList(SortingCenterModel model)
        {
            var total = 0;
            var list = (from a in ServiceFactory.SortingCenterServiceInstance.GetPagingList(model, out total).AsNoTracking()
                        join b in ServiceFactory.D_ProvinceServiceInstance.LoadEntities(t => true) on a.ProvinceId equals b.ProvinceID into temp1
                        from c in temp1.DefaultIfEmpty()
                        join d in ServiceFactory.D_CityServiceInstance.LoadEntities(t => true) on a.CityId equals d.CityID into temp2
                        from e in temp2.DefaultIfEmpty()
                        select new
                        {
                            a.ID,
                            a.SortingCenterName,
                            a.IsAvailable,
                            a.LastModifyBy,
                            a.LastModifyTime,
                            c.ProvinceName,
                            e.CityName
                        }).ToList();
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 分拣中心编辑 + ActionResult SortingCenterEdit(int id)
        /// <summary>
        /// 分拣中心编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult SortingCenterEdit(int id)
        {
            ViewBag.ShowTitle = id > 0 ? "编辑分拣中心" : "添加分拣中心";
            var SortingCenter = ServiceFactory.SortingCenterServiceInstance.LoadByID(id);
            return View(SortingCenter);
        }

        #endregion

        #region 保存分拣中心 + ActionResult SaveSortingCenter(SortingCenter model)

        [HttpPost]
        [ValidateInput(false)]
        //保存分拣中心
        public ActionResult SaveSortingCenter(SortingCenter model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };

            var serv = ServiceFactory.SortingCenterServiceInstance;
            model.SortingCenterName = model.SortingCenterName.Trim();
            if (model.ID == 0)
            {
                if (serv.IsExist(model.SortingCenterName))
                {
                    responseResult.Message = "该分拣中心已经存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            else
            {
                var entity = serv.LoadByID(model.ID);
                if (entity != null)
                {
                    if (entity.SortingCenterName != model.SortingCenterName && serv.IsExist( model.SortingCenterName, model.ID))
                    {
                        responseResult.Message = "已存在同名的分拣中心，请修改分拣中心名称";
                    }
                    entity = ReflectHelper<SortingCenter, SortingCenter>.CloneT1ToT2(model, entity);
                    entity.LastModifyBy = CurrentUser.UserAccount;
                    entity.LastModifyTime = DateTime.Now;
                    responseResult.Success = ServiceFactory.SortingCenterServiceInstance.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "该分拣中心不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add && responseResult.Success ? RedirectToAction("SortingCenterIndex") : RedirectToAction("SortingCenterEdit", new { id = model.ID });
        }


        #endregion

        #region 禁用/启用分拣中心 + JsonResult UpdateStatus(int id, bool isAvailable)
        /// <summary>
        /// 禁用/启用分拣中心
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="isAvailable">启用/禁用</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.SortingCenterServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                entity.LastModifyBy = CurrentUser.UserAccount;
                entity.LastModifyTime = DateTime.Now;
                success = ServiceFactory.SortingCenterServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
        #endregion
    }
}